import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-03-12
 * Time: 15:19
 */
public class evalRPN {
    String[] tokens = {"2","1","+","3","*"};
    Stack<Integer> stack = new Stack<>();
    public int  ergodic() {
        //遍历这个数组
        for (int i = 0; i < tokens.length; i++) {
            //判断字符串是否为运算符字符串
            String str = tokens[i];
            if(!isOperator(str)) {
                //字符串为数字，说明将这个字符串数字先转化为数字再压入栈中
                int ret = Integer.valueOf(str);
                stack.push(ret);
            }
            else {
                //字符串为运算符
                int rightNum = stack.pop();
                int leftNum = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(leftNum+rightNum);
                        break;
                    case "-":
                        stack.push(leftNum-rightNum);
                        break;
                    case "*":
                        stack.push(leftNum*rightNum);
                        break;
                    case "/":
                        stack.push(leftNum/rightNum);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperator(String str) {
        if(str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") ) {
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        evalRPN evalRPN = new evalRPN();
        System.out.println(evalRPN.ergodic());
    }
}
